package com.brother.template.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by xjw on 2017/8/22.
 */
public class ResponeUtil {


    public static void sendFail(HttpServletResponse response, Object message) {
        sendFail(response, message, null, false);
    }

    public static void sendFail(HttpServletResponse response, Object message, String key) {
        sendFail(response, message, key, false);
    }

    public static void sendFail(HttpServletResponse response, Object message, boolean notNull) {
        sendFail(response, message, null, notNull);
    }

    /**
     * 返回错误
     *
     * @param response
     * @param message
     * @param key
     * @param notNull  true 没有null false 有null
     */
    public static void sendFail(HttpServletResponse response, Object message, String key, boolean notNull) {
        Map<String, Object> map = new HashMap<>();
        map.put("success", false);
        if (key == null) {
            key = "msg";
        }
        if (message != null) {
            map.put(key, message);
        }
        if (notNull) {
            writerObjectNotNull(response, map);
        } else {
            writerObject(response, map);
        }
    }

    public static void sendSuccess(HttpServletResponse response, Object message) {
        sendSuccess(response, message, null, false);
    }

    public static void sendSuccess(HttpServletResponse response, Object message, String key) {
        sendSuccess(response, message, key, false);
    }

    public static void sendSuccess(HttpServletResponse response, Object message, boolean notNull) {
        sendSuccess(response, message, null, notNull);
    }

    /**
     * 返回正确
     *
     * @param response
     * @param message
     * @param key
     * @param notNull  ture 没有null false 有null
     */
    public static void sendSuccess(HttpServletResponse response, Object message, String key, boolean notNull) {
        Map<String, Object> map = new HashMap<>();
        map.put("success", true);
        if (message != null) {
            if (key == null) {
                key = "data";
            }
            map.put(key, message);
        }
        if (notNull) {
            writerObjectNotNull(response, map);
        } else {

            writerObject(response, map);
        }

    }

    /**
     * 属性中有null的返回
     *
     * @param response
     * @param message
     */
    public static void writerObject(HttpServletResponse response, Object message) {
        if (message != null) {
            String result = JSON.toJSONString(message, SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteMapNullValue);
            response.setHeader("Content-Type", "application/json;charset=UTF-8");
            try {
                response.getWriter().write(result);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }

    /**
     * 属性中有null的不返回
     *
     * @param response
     * @param message
     */
    public static void writerObjectNotNull(HttpServletResponse response, Object message) {
        if (message != null) {
            String result = JSON.toJSONString(message, SerializerFeature.DisableCircularReferenceDetect);
            response.setHeader("Content-Type", "application/json;charset=UTF-8");
            try {
                response.getWriter().write(result);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }
}
